import Vue from "vue"
import zheng from '../api/protal.js'
import store from '../store/index.js'
/**
 *
 *
 */
const projectMark = 'gociif'; // 每个项目自己配置
const secret = 'b6ab8a6b5a5b45459018bef307328d36'; // 每个项目自己配置
const session_id = new Date().getTime() + ''
const loginUserId = uni.getStorageSync('loginUserId') || parseInt(Math.random() * 1000000) + '' + new Date().getTime()
uni.setStorageSync('loginUserId', loginUserId)

// #ifdef H5
const jsAPI = (eventType, eventKey, params) => {
	const href = location.href
	const hostname = location.hostname

	const from_searchs = [
		['百度', 'www.baidu.com'],
		['谷歌', 'www.google.com'],
		['搜狗', 'www.sogou.com'],
		['bing', 'cn.bing.com'],
		['神马搜索', 'm.sm.cn'],
		['360搜索', 'www.so.com'],
		['头条', 'so.toutiao.com']
	] // 搜索引擎来源
	const referrer = document.referrer || location.href
	let fromdomain = location.hostname
	if (referrer && referrer.split('//')[1]) {
		fromdomain = referrer.split('//')[1].split('/')[0]
	}
	try {
		const pubParams = {
			from_type: document.referrer ? 'search' : 'direct_access',
			fromdomain,
			fromurl: referrer,
			from_search: from_searchs.find(x => referrer.includes(x[1])) ? from_searchs.find(x => referrer
				.includes(x[1]))[0] : '',
			visitdomain: location.hostname,
			page_name: location.href,
			landingpageurl: href,
			landingdomain: hostname,
			is_new_visitor: uni.getStorageSync('loginUserId') ? false : true
		}
		let platfrom = 'XCX'
		// #ifdef H5
		platfrom = 'H5'

		if (store.state.deviceType === 'pc') {
			platfrom = 'PC'
		}
		// #endif
		// api.receivepush({
		// 	projectMark,
		// 	eventTime: new Date().getTime(),
		// 	eventKey,
		// 	eventType: eventType === 'track' ? 2 : 1,
		// 	eventVariable: {
		// 		...params,
		// 		...pubParams,
		// 		businessLinesMark: 'gociif_applet', // 云展 yz  预热h5 hoth5
		// 		session_id,
		// 		platfrom
		// 	},
		// 	loginUserId,
		// }, {
		// 	projectMark,
		// 	secret // 每个项目自己配置
		// });
	} catch (err) {
		console.log(err, '埋点')
	}
};
// #endif
// #ifndef H5
const jsAPI = (eventType, eventKey, params) => {
	const pages = getCurrentPages()
	const page = pages[pages.length - 1]
	const href = page && page.$page ? page.$page.fullPath : ''
	const hostname = page && page.$page ? page.$page.fullPath : ''

	const from_searchs = '' // 搜索引擎来源
	const referrer = page && page.$page ? page.$page.fullPath : ''
	let fromdomain = page && page.$page ? page.$page.fullPath : ''
	if (referrer && referrer.split('//')[1]) {
		fromdomain = referrer.split('//')[1].split('/')[0]
	}

	try {
		const pubParams = {
			from_type: 'direct_access',
			fromdomain,
			fromurl: referrer,
			from_search: '',
			visitdomain: page && page.$page ? page.$page.fullPath : '',
			page_name: page && page.$page ? page.$page.fullPath : '',
			landingpageurl: href,
			landingdomain: hostname,
			is_new_visitor: uni.getStorageSync('loginUserId') ? false : true
		}
		let platfrom = 'XCX'
		// #ifdef H5
		platfrom = 'H5'

		if (store.state.deviceType === 'pc') {
			platfrom = 'PC'
		}
		// #endif

		// api.receivepush({
		// 	projectMark,
		// 	eventTime: new Date().getTime(),
		// 	eventKey,
		// 	eventType: eventType === 'track' ? 2 : 1,
		// 	eventVariable: {
		// 		...params,
		// 		...pubParams,
		// 		businessLinesMark: 'gociif_applet',
		// 		session_id,
		// 		platfrom
		// 	},
		// 	loginUserId,
		// }, {
		// 	projectMark,
		// 	secret // 每个项目自己配置
		// });
	} catch (err) {
		console.log(err, '埋点')
	}
};
// #endif

Vue.prototype.$jsAPI = jsAPI;